package com.jckl.foodathon.restlet.services;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jckl.foodathon.common.constant.ApplicationConstants;
import com.jckl.foodathon.common.util.GeoLocation;
import com.jckl.foodathon.dao.StoreDAO;
import com.jckl.foodathon.model.Response;
import com.jckl.foodathon.model.Store;


/**
 * Resource which has only one representation.
 * 
 */
public class TestResource extends ServerResource {
	
	private static final Logger log = Logger.getLogger(TestResource.class.getName());


    @Post("json")
    public String addStore(Representation entity) {
    	Response response = new Response();
    	response.setServerResponse(ApplicationConstants.SERVER_RESP_FAIL);
    	Gson gson = new Gson();
    	
    	boolean validationPass = true;
    	
    	try{
    		JSONObject req = (new JsonRepresentation(entity)).getJsonObject();
    		
    		log.info("Incoming JSON: "+req.toString());
    		Store store = gson.fromJson(req.toString(), Store.class);
    		
    		//Setting the GeoCell
    		GeoLocation geoLocation = new GeoLocation();
    		store.setPoint(geoLocation.getGeoPt(store.getLatitude(), store.getLongitude()));
    		store.setGeoCells(geoLocation.getGeoCells(store.getLatitude(), store.getLongitude()));
			
			//Setting the system data
			store.setCreatedDate(new Date());
			store.setUpdatedDate(new Date());
			store.setActive(true);
    		
    		//Add store into datasource
    		StoreDAO storeDao = new StoreDAO();
    		
    		if(storeDao.getStoreByName(store.getName())!=null){
    			response.setServerResponse(ApplicationConstants.SERVER_RESP_FAIL);
    			response.setRemark("Existing store name found!");
    			validationPass = false;
    		}
    		
    		if(validationPass){
    			if(storeDao.newStore(store)){
        			//Update store successfully
        			response.setServerResponse(ApplicationConstants.SERVER_RESP_SUCCESS);
            		log.info("Store '"+store.getName()+"' successfully added into datastore");
        		}else{
        			log.severe("Store '"+store.getName()+"' unable to add into datastore");
        		}
    		}
    		
    	}catch(JsonSyntaxException jsonSyntaxEx){
    		jsonSyntaxEx.printStackTrace();
    	}catch(JSONException jsonEx){
    		jsonEx.printStackTrace();
    	}catch(IOException ioEx){
    		ioEx.printStackTrace();
    	}
    	return gson.toJson(response);
    }
    
    @Get
    public void addStoreDefault() {
    	
    	boolean validationPass = true;
    	
    	try{
    		Store store = new Store();
    		store.setLatitude(3.202508);
    		store.setLongitude(101.67427);
    		store.setName("Jalan Ipoh");
    		
    		//Setting the GeoCell
    		GeoLocation geoLocation = new GeoLocation();
    		store.setPoint(geoLocation.getGeoPt(store.getLatitude(), store.getLongitude()));
    		store.setGeoCells(geoLocation.getGeoCells(store.getLatitude(), store.getLongitude()));
			
			//Setting the system data
			store.setCreatedDate(new Date());
			store.setUpdatedDate(new Date());
			store.setActive(true);
    		
    		//Add store into datasource
    		StoreDAO storeDao = new StoreDAO();
    		
    		if(storeDao.getStoreByName(store.getName())!=null){
    			validationPass = false;
    		}
    		
    		if(validationPass){
    			if(storeDao.newStore(store)){
        			//Update store successfully
            		log.info("Store '"+store.getName()+"' successfully added into datastore");
        		}else{
        			log.severe("Store '"+store.getName()+"' unable to add into datastore");
        		}
    		}
    		
    	}catch(JsonSyntaxException jsonSyntaxEx){
    		jsonSyntaxEx.printStackTrace();
    	}
    	
    	try{
    		Store store = new Store();
    		store.setLatitude(3.16154);
    		store.setLongitude(101.71996);
    		store.setName("Hallmark");
    		
    		//Setting the GeoCell
    		GeoLocation geoLocation = new GeoLocation();
    		store.setPoint(geoLocation.getGeoPt(store.getLatitude(), store.getLongitude()));
    		store.setGeoCells(geoLocation.getGeoCells(store.getLatitude(), store.getLongitude()));
			
			//Setting the system data
			store.setCreatedDate(new Date());
			store.setUpdatedDate(new Date());
			store.setActive(true);
    		
    		//Add store into datasource
    		StoreDAO storeDao = new StoreDAO();
    		
    		if(storeDao.getStoreByName(store.getName())!=null){
    			validationPass = false;
    		}
    		
    		if(validationPass){
    			if(storeDao.newStore(store)){
        			//Update store successfully
            		log.info("Store '"+store.getName()+"' successfully added into datastore");
        		}else{
        			log.severe("Store '"+store.getName()+"' unable to add into datastore");
        		}
    		}
    		
    	}catch(JsonSyntaxException jsonSyntaxEx){
    		jsonSyntaxEx.printStackTrace();
    	}
    }
    
}
